<!DOCTYPE html>
<html class="loading" lang="en" data-textdirection="ltr">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimal-ui">
	<meta name="author" content="PIXINVENT">
	<title>999后台管理系统</title>
	<link rel="stylesheet" type="text/css" href="app-assets/css/vendors.css">
	<link rel="stylesheet" type="text/css" href="app-assets/css/vertical-menu.css">
	<link rel="stylesheet" type="text/css" href="app-assets/css/bootstrap-extended.css">
	<script src="app-assets/js/Dream.js"></script>
	<script src="app-assets/js/Http.js"></script>
</head>
<body>
<div class="content-wrapper">
	<div class="content-header ">
		<div class="content-header-left mb-2">
			<h3 class="content-header-title mb-0">订单结算管理</h3>
		</div>
	</div>
	<div class="card">
		<div class="card-content">
			<div class="card-body">
				<div class="searchbox">
					<div class="form-inline">
						<div class="form-group">
							<input type="text" class="form-control" id="orderId" placeholder="订单编号">
						</div>
						<div class="form-group">
							<button type="submit" class="btn btn-primary" onclick="selects()">查询</button>
						</div>
					</div>
				</div>
				<div class="tablebox1">
					<table class="table">
						<thead>
						<tr>
							<th>订单编号</th>
							<th>金额</th>
							<th>订单日期</th>
							<th>状态</th>
							<th>操作</th>
						</tr>
						</thead>
						<tbody id="showT">
						<tr>
						</tr>

						</tbody>
					</table>

					<div class="page clearfix">
						<div class="pull-left">共 <span id="total"></span> 条记录 第 <span id="pageNum"></span> / <span
								id="pages"></span> 页
						</div>
						<div class="pull-right">

							<nav aria-label="..." class="pull-left">
								<ul class="pagination" id="pageTo">

								</ul>
							</nav>
							<div class="pull-left p-l-20"><a class="" href="#" onclick="pageTo()">跳至</a><input
									type="text" value="" class="form-control" id="pageNumber">页
							</div>
						</div>

					</div>

				</div>
			</div>
		</div>
	</div>
</div>



<script src="app-assets/js/vendors.min.js" type="text/javascript"></script>
<script src="app-assets/js/app-menu.js" type="text/javascript"></script>
<script src="app-assets/js/app.js" type="text/javascript"></script>
<script src="app-assets/js/echarts.min.js" type="text/javascript"></script>
<script>
	$(document).ready(function () {
		$(".ewm").click(function () {
			$(this).toggleClass("active")
		});
		selectAll($('#orderId').val(), 1)
	});

	function selectAll(poId,pageNum) {
		$.ajax({
			'url': HTTP.getUserInfo+'/purchaseSettlement',
			'type': 'get',
			'dataType': 'json',
			'data': {poId,pageNum},
			'success': selectAllSuccess
		})
	}

	function selects() {
		selectAll($('#orderId').val(), 1)
	}

	function selectAllSuccess(data) {
		$('#showT tr').remove()
		data.data.list.forEach(dd => {
			let status = dd.settlementStatus == '0' ? '<span class="text-danger">待收货</span>' : '<span class="text-primary">已收货</span>'
			let bt = ``
			if (dd.settlementStatus == '1') {
				bt = `<input type="button" class="btn btn-light" value="已结算">`
			}else {
				bt = `<input type="button" class="btn btn-primary" onclick="delivery(`+dd.poId+`)" value="收货结算">`
			}
			$('#showT').append(`<tr>
                    <td>${dd.poId}</td>
                    <td>￥${dd.totalAmount}</td>
                    <td>`+formatDate(dd.settlementDate)+`</td>
                    <td>` + status + `</td>
                    <td>
						`+bt+`
                    </td>
                </tr>`)
		})
		$('#total').html(data.data.total)
		$('#pageNum').html(data.data.pageNum)
		$('#pages').html(data.data.pages)
		$('#pageSize').html(data.data.pageSize)
		let upP = ''
		if (data.data.pageNum == 1) {
			upP = 'disabled'
		}
		let neP = ''
		if (data.data.pageNum == data.data.pages) {
			neP = 'disabled'
		}
		let pageTo = ''
		pageTo += `<li class="` + upP + `" onclick="upPage()"><a href="#" aria-label="Previous"><span aria-hidden="true">«</span></a></li>`
		for (let i = 1; i <= data.data.pages; i++) {
			if (i == data.data.pageNum) {
				pageTo += `<li class="active"><a href="#">${i} <span class="sr-only">(current)</span></a></li>`
			} else {
				pageTo += `<li><a href="#">${i}</a></li>`
			}
		}
		pageTo += `<li class="` + neP + `" onclick="nePage()"><a href="#" aria-label="Next"><span aria-hidden="true">»</span></a></li>`
		$('#pageTo').html(pageTo)

		$('#pageTo a').click(function () {
			let pageNum = $(this).html()
			selectAll($('#orderId').val(),pageNum)
		})
	}

	function upPage() {
		let pageNum = $('#pageNum').html()
		pageNum--
		selectAll($('#orderId').val(), pageNum)
	}

	function nePage() {
		let pageNum = $('#pageNum').html()
		pageNum++
		selectAll($('#orderId').val(),pageNum)
	}

	function pageTo() {
		let pageNum = $('#pageNumber').val()
		selectAll($('#orderId').val(),pageNum)
	}


	function delivery(poId) {
		let updatedBy = sessionStorage.getItem("loginName")
		$.ajax({
			'url': HTTP.getUserInfo+'/purchaseSettlement',
			'type': 'put',
			'dataType': 'json',
			'contentType': 'application/json',
			'data': JSON.stringify({poId, updatedBy}),
			'success': deliverySuccess
		})
	}
	function deliverySuccess(data) {
		if (data.code  == 200){
			Dreamer.success(data.message)
			selectAll($('#orderId').val(), 1)
		}else {
			Dreamer.error(data.message)
		}
	}



	function formatDate(date) {
		var date = new Date(date);
		var date =  new Date(date.getTime() - (8 * 60 * 60 * 1000));
		var year = date.getFullYear();
		var month = (date.getMonth() + 1).toString().padStart(2, '0'); // 月份是从0开始的
		var day = date.getDate().toString().padStart(2, '0');
		var hours = date.getHours().toString().padStart(2, '0');
		var minutes = date.getMinutes().toString().padStart(2, '0');
		var seconds = date.getSeconds().toString().padStart(2, '0');

		// 返回格式化的日期时间字符串
		return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
	}
</script>
</body>
</html>